import mountElement from "./mountElement";
import updateNodeElement from "./updateNodeElement";

export default function createDOMElement(virtualDOM){
    let newElement = null
    if(virtualDOM.type === 'text'){
        // 普通文本节点
        newElement = document.createTextNode(virtualDOM.props.textContent)
    } else {
        // 元素节点
        newElement = document.createElement(virtualDOM.type)
        updateNodeElement(newElement,virtualDOM)
    }

    newElement._virtualDOM = virtualDOM // 缓存virtualDOM对象到新的真实DOM上 方便后续的新旧DOM的对比逻辑
    // 递归创建子节点
    virtualDOM.children.forEach(child => {
        mountElement(child, newElement)
    });
    if(virtualDOM.props && virtualDOM.props.ref){
        virtualDOM.props.ref(newElement)
    }
    return newElement
}